//
//  ViewController.m
//  qq
//
//  Created by 李凯宁 on 16/2/14.
//  Copyright © 2016年 李凯宁. All rights reserved.
//

#import "ViewController.h"
#import "TableViewCell.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSources;
@end

@implementation ViewController



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//
//
//    nihao a 
//
//
    
}


// NSOutputStream 写入(拼接)文件
-(void)test2
{
    NSString *path = @"";
    
    NSData *data = nil;
    
    NSOutputStream *stream = [[NSOutputStream alloc] initToFileAtPath:path append:YES];
    
    [stream open];
    
    [stream write:data.bytes maxLength:data.length];
    
    [stream close];
}


// NSFileHandle 写入(拼接)文件
- (void)test1
{
    NSString *path = @"";
    
    NSData *data = nil;
    
    NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
    
    [handle seekToEndOfFile];
    
    [handle writeData:data];
    
    [handle closeFile];
}



// 创建并且开启一个网络连接/同时设置代理对象
-(void)test
{
    NSURL *url = [NSURL URLWithString:@""];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
    
    [conn start];
}


























- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.dataSources = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    __weak typeof(self) weakSelf = self;
    [manager GET:@"http:/app.interface.jescard.com/goods/getGoodsListByPage.html?brandId=8aae246252aba7870152abb0ba2a0000&pagesize=20&page=16" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        if (strongSelf) {
            strongSelf.dataSources = [[responseObject objectForKey:@"obj"]objectForKey:@"data"];
            [strongSelf.tableView reloadData];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      
        NSLog(@"%@",error);
        
    }];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSources.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [TableViewCell cell];
    }
    NSString *urlString = [self.dataSources[indexPath.row] objectForKey:@"goodsPicture"];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"Icon"] options:SDWebImageLowPriority];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.width / 3;
}

- (void)didReceiveMemoryWarning {
    NSLog(@"warning");
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
